package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService  empService;
    @Autowired
    @Lazy
    private StudentService studentService;

    @Override
    public PageResult<Clazz> getpageResult(ClazzQueryParam param) {
        //获取分页查询参数
        Page<Clazz> page = Page.of(param.getPage(), param.getPageSize());
        //执行查询
        page = lambdaQuery()
                .like(param.getName() !=null && ! param.getName().isEmpty(),Clazz::getName, param.getName())
                .between(param.getBegin() != null && param.getEnd() != null,Clazz::getEndDate, param.getBegin(), param.getEnd())
                .page(page);
        //获取当前查询结果
        List<Clazz> clazzList = page.getRecords();
        //采用stream流将当前查询结果中的masterId查询出来，并封装到Set集合中
        if (!clazzList.isEmpty()){
            HashSet<Integer> masterIds =(HashSet<Integer>) clazzList.stream().map(Clazz::getMasterId).collect(Collectors.toSet());
            //根据masterId查询出对应的员工姓名
            List<Emp> emps = empService.listByIds(masterIds);
            if (!emps.isEmpty()){
                //将员工的id及name封装到Map集合中
                Map<Integer, String> masterMap = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
                //遍历班级集合，为每一个班级设置masterName属性及status属性
                clazzList.forEach(clazz -> {
                    //设置班主任名字
                    clazz.setMasterName(masterMap.get(clazz.getMasterId()));
                    //获取班级的开课时间
                    LocalDate beginDate = clazz.getBeginDate();
                    //获取班级的结课时间
                    LocalDate endDate = clazz.getEndDate();
                    LocalDate now = LocalDate.now();
                    if (now.isBefore(endDate) && now.isAfter(beginDate)) {
                        clazz.setStatus("在读");
                    } else if (now.isBefore(beginDate)) {
                        clazz.setStatus("未开课");
                    } else {
                        clazz.setStatus("已结课");
                    }
                });
            }
        }
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public void removeClazzById(Integer id) {
        //根据班级id查询班级学生数量
        long count = studentService.count(new LambdaQueryWrapper<Student>().eq(Student::getClazzId, id));

        if (count > 0) {
            throw new RuntimeException("当前班级有学生，不能删除");
        }
    }


}
